package com.seven.springboot.datasource;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.seven.springboot.enums.DatabaseType;

@Aspect
@Component
//@Order(-100)
public class DataSourceAspect {
	
	private static final Logger logger = LoggerFactory.getLogger(DataSourceAspect.class);

	/**
     * 使用空方法定义切点表达式
     */
    @Pointcut("execution(* com.seven.springboot.service.yin.*.*(..))")
    public void yinJointPointExpression() {
    	
    }
    
    @Pointcut("execution(* com.seven.springboot.service.yang.*.*(..))")
    public void yangJointPointExpression() {
    	
    }

    /**
     * 使用定义切点表达式的方法进行切点表达式的引入
     */
    @Before("yinJointPointExpression()")
    public void setYinDataSourceKey(JoinPoint point) {
    	logger.info("切换到yin数据源。。。");
       DatabaseContextHolder.setDatabaseType(DatabaseType.yin);
    }
    
    @Before("yangJointPointExpression()")
    public void setYangDataSourceKey(JoinPoint point) {
    	logger.info("切换到yang数据源。。。");
       DatabaseContextHolder.setDatabaseType(DatabaseType.yang);
    }

}
